package com.example.androidproject.movie.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidproject.R;
import com.example.androidproject.app.ClassifyActivity3;
import com.example.androidproject.movie.bean.ResultBeanData;
import com.example.androidproject.novel.bean.NovelBean;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

public class MovieRecyclerViewAdapter extends RecyclerView.Adapter {
    private final String TAG = "GGSTUDY";
    //* 这些类型 是指 这个页面的 没有个模块， 比如最上面的是轮播图广告 接下来是热门电影，之后是高评分电影 等等
    //* 现在就写两个类型 一个是轮播图 一个是主体，至于其他的什么热门什么的 后期再来
    // 轮播图可以用 viewPage写 也可以用第三方库banner 这里的话就先用第三方库来实现，后期在用一个viewpage来实现一次
    /**
     * 轮播图广告
     */
    public static final int BANNER = 0;
    /**
     * 频道
     */
    public static final int CHANNEL = 1;
    /**
     * 主体内容 这个内容不写在这里 // body内容没有写在recyclerview里面
     */
    public static final int BODYCONTENT = 2;

    /**
     * 当前类型 默认是轮播图广告
     */
    public int currentType = BANNER;

    private Context mContext;
    private List<ResultBeanData.DataBean> data;
    //
    private List<NovelBean.DataBean> dataNovel;
    private LayoutInflater layoutInflater;
    private int flag;


    public MovieRecyclerViewAdapter(Context mContext, List<ResultBeanData.DataBean> data, int flag) {
        this.mContext = mContext;
        this.data = data;
        this.flag = flag;
        layoutInflater = LayoutInflater.from(mContext);
    }

    //
    public MovieRecyclerViewAdapter(Context mContext, ArrayList<NovelBean.DataBean> data, int flag) {
        this.mContext = mContext;
        this.dataNovel = data;
        this.flag = flag;
        layoutInflater = LayoutInflater.from(mContext);
    }

    /**
     * 相当于getView
     * 创建ViewHolder
     *
     * @param parent
     * @param viewType 当前的类型
     * @return
     */
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == BANNER) {
            return new BannerViewHolder(mContext, layoutInflater.inflate(R.layout.banner_viewpapger, null));
        } else if (viewType == CHANNEL) {
            return new ChannelViewHolder(mContext, layoutInflater.inflate(R.layout.channel_layout, null));
        } else if (viewType == BODYCONTENT) {
            // body内容没有写在recyclerview里面 这里可以删掉
            return new BodyContentHolder(mContext, layoutInflater.inflate(R.layout.movie_body, null));
        }
        return null;
    }

    /**
     * 相当于getView中的绑定数据模块
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == BANNER) {
            BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
            bannerViewHolder.setData(data);
        } else if (getItemViewType(position) == CHANNEL) {
            ChannelViewHolder channelViewHolder = (ChannelViewHolder) holder;
            channelViewHolder.setData();
        } else if (getItemViewType(position) == BODYCONTENT) {
            // body内容没有写在recyclerview里面 这里可以删掉
            BodyContentHolder bodyContentHolder = (BodyContentHolder) holder;
            bodyContentHolder.setData(data);
        }

    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        private final Context mContext;
        private final Banner banner;

        public BannerViewHolder(Context mContext, View itemView) {
            super(itemView);
            this.mContext = mContext;
            this.banner = itemView.findViewById(R.id.banner);
        }

        public void setData(List<ResultBeanData.DataBean> data) {
            List<String> urlList = new ArrayList<>();
            if (flag == 1) {
                for (int i = 0; i < data.size(); i++) {
                    String imageUrl = data.get(i).getSrc();
                    urlList.add(imageUrl);
                }
            }
            //
            if (flag == 2) {
                for (int i = 0; i < dataNovel.size(); i++) {
                    String imageUrl = dataNovel.get(i).getImage();
                    urlList.add(imageUrl);
                }
            }
            // 设置banner数据
            /**
             * 使用自定义的Adapter
             * // 魅族效果
             *  banner.setBannerGalleryMZ(60,0.8f);
             * //画廊效果
             * banner.setBannerGalleryEffect(int,int,float)
             */
            banner.addBannerLifecycleObserver((LifecycleOwner) mContext)
                    .setAdapter(new BannerImageAdapter1(urlList, mContext))
                    .setIndicator(new CircleIndicator(mContext));
            banner.setBannerRound(30);
            banner.setBannerGalleryMZ(90, 0.6f);
//            banner.setBannerGalleryEffect(30, 100, 0.5f);
            //设置点击事件 这里还没有效果 需要解决
            banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(Object data, int position) {
                    // data -> 图片的网络地址
                    Log.d(TAG, "onClick: " + "点击了banner" + position + data);
                }
            });
            /**
             * 自带的adapter
             */
//            banner.setAdapter(new BannerImageAdapter<String>(urlList) {
//                @Override
//                public void onBindView(BannerImageHolder holder, String data, int position, int size) {
//                    Glide.with(holder.itemView)
//                            .asBitmap()
//                            .load(data)
////                            .override(300, 300)
////                            .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
//                            .into(new CustomTarget<Bitmap>() {
//                                @Override
//                                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
//                                    int imageWidth = resource.getWidth();
//                                    int imageHeight = resource.getHeight();
//                                    int height = ScreenUtils.getScreenWidth(mContext) * imageHeight / imageWidth;
//                                    ViewGroup.LayoutParams para = holder.imageView.getLayoutParams();
//                                    para.height = height;
//                                    para.width = ScreenUtils.getScreenWidth(mContext);//屏幕的宽度，没有工具类自己从网上搜
//                                    holder.imageView.setImageBitmap(resource);
//                                }
//
//                                @Override
//                                public void onLoadCleared(@Nullable Drawable placeholder) {
//                                }
//                            });
//                }
//            });
            banner.start();
        }
    }

    class ChannelViewHolder extends RecyclerView.ViewHolder {
        private final Context mContext;
        private final GridView gv_channel;
        private ChannelAdapter adapter;

        public ChannelViewHolder(Context mContext, View itemView) {
            super(itemView);
            this.mContext = mContext;
            this.gv_channel = itemView.findViewById(R.id.gv_channel);
        }

        public void setData() {
            List<String> textList = new ArrayList<>();
            textList.add("分类");
            textList.add("收藏");
            adapter = new ChannelAdapter(mContext, textList);
            gv_channel.setAdapter(adapter);
            gv_channel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(mContext, ClassifyActivity3.class);
                    Bundle bundle = new Bundle();
                    switch (position) {
                        case 0:
                            if (flag == 1) {
                                // 电影
                                bundle.putInt("flag", 1);
                            }
                            if (flag == 2) {
                                // 小说
                                bundle.putInt("flag", 2);
                            }
                            intent.putExtras(bundle);
                            mContext.startActivity(intent);
                            break;
                        case 1:
                            Toast.makeText(mContext, "channel点击了：" + 2, Toast.LENGTH_SHORT).show();
                            break;
                        default:
                            Toast.makeText(mContext, "channel点击了：" + position, Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    // body内容没有写在recyclerview里面 这里可以删掉
    class BodyContentHolder extends RecyclerView.ViewHolder {
        private final String TAG = "GGSTUDY";
        private final GridView gv_movie_body;
        private final Context mContext;

        public BodyContentHolder(Context mContext, View itemView) {
            super(itemView);
            this.mContext = mContext;
            this.gv_movie_body = itemView.findViewById(R.id.gv_movie_body);
        }

        public void setData(List<ResultBeanData.DataBean> data) {

        }
    }

    /**
     * 自己重写
     * 得到类型
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case BANNER:
                currentType = BANNER;
                break;
            case CHANNEL:
                currentType = CHANNEL;
                break;
            case BODYCONTENT:
                // body内容没有写在recyclerview里面 这里可以删掉
                currentType = BODYCONTENT;
                break;
        }
        return currentType;
    }

    /**
     * 总共有多少个item
     *
     * @return
     */
    @Override
    public int getItemCount() {
        // 有多少个模块就返回多少
        // 模块 -> 广告，热门那些
        return 2;
    }
}
